<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');?>
<div class="middle padtop10 myclear">
	<div class="page-content">
		
		<!-- bread crumb -->
		<p id="bread-crumb">
			<a href="<?php echo base_url();?>"><?php echo $lang['home']?></a>
			<?php echo $lang['agency']?>
		</p><!-- end bread crumb -->
		
		<h3>
			<?php echo $lang['agency']?>
		</h3>
		<br style="clear:both;" />
		
		<?php
		$msg = trim($this->input->get('msg',true));
		if($msg != ''){
		?>
		<p class="msg"><?php echo $msg;?></p>
		<br />
		<?php }?>
		
		<div>
			<p style="text-align:right">
				<input type="button" value="<?php echo $lang['add']?>" onclick="window.location.href='<?php echo site_url('user/addgroup')?>'" />
			</p>
			<table cellpadding="0" cellspacing="0" width="100%" border="1" class="martop10 tbl-list">
			<tr>
				<td class="td-label text-center" width="25"><strong>#</strong></td>
				<td class="td-label" width="150"><strong><?php echo $lang['name']?></strong></td>
				<td class="td-label"><strong><?php echo $lang['description']?></strong></td>
				<td class="td-label text-center"><strong><?php echo $lang['allowfree']?></strong></td>
				<td class="td-label text-center" width="60"><strong><?php echo $lang['ispostpaid']?></strong></td>
				<td class="td-label text-center"><strong><?php echo $lang['discount']?> (%)</strong></td>
				<td class="td-label text-center"><strong><?php echo $lang['balance']?> (VND)</strong></td>
				<td class="td-label text-center"><strong><?php echo $lang['totalbalance']?> (VND)</strong></td>
				<td class="td-label text-center"><strong><?php echo $lang['promotion']?> (VND)</strong></td>
                <td class="td-label text-center"><strong>User</strong></td>
                <td class="td-label text-center"><strong>Disabled</strong></td>
			</tr>
			<?php
			$i = 0;
			foreach ($items as $item)
			{
				$temp=0;
				if(isset($lstDisable[$item['groupid']]))
				{
					$temp = $lstDisable[$item['groupid']]['num'];
				}
			?>
				<tr <?php echo ($temp==0 && $item['groupid'] > 0) ? 'style="background-color: #d3d3d3;"':'';?>>
					<td class="text-center"><?php echo $i+1?></td>
					<td><a href="<?php echo site_url('user/editgroup/?gid='.$item['groupid'])?>"><?php echo $item['groupname']?></a></td>
					<td><?php echo $item['description']?></td>
					<td class="text-center"><?php echo $lang['bit'][$item['allowfree']]?></td>
					<td class="text-center"><?php echo $lang['arr_ispostpaid'][$item['ispostpaid']]?></td>
					<td class="text-right"><?php echo number_format($item['discounts'])?></td>
					<td class="text-right"><?php echo number_format($item['balance'])?></td>
					<td class="text-right"><?php echo number_format($item['totalbalance'])?></td>
					<td class="text-right"><?php echo number_format($item['promotions'])?></td>
	                <td class="text-right"><?php echo $temp;?></td>
	                <td class="text-right">
						<?php 
						if($temp > 0 && $item['groupid'] > 0)
	                	{
	               		?>
	                    	<input type="button" value="disabled" onclick="disabled_agency('<?php echo $item['groupid'];?>');"/>
	                	<?php
						}
						?>
					</td>
	            </tr>
			<?php
				$i++;
			}
			?>
		</table>
			<ul style="margin-left:40px;">
			<?php if(false) foreach ($items as $row){?>
				<li>
					<a href="<?php echo site_url('user/listgroup/?gid='.$row['groupid'])?>">
						<?php echo $row['groupname']?>
					</a>
				</li>
			<?php }?>
			</ul>
		</div>
		<div style="width:40%;float:right; display:none">
			<form name="addGroup" action="<?php echo site_url('user/savegroup')?>" method="post" onsubmit="return checkSubmit();">
				<p class="login-label">
					<?php echo $lang['groupname']?>: <span class="require">*</span>
				</p>
				<p class="padbottom2">
					<input type="text" class="login-input" name="groupname" id="groupname" value="<?php echo $group['name']?>" />
				</p>
				
				<p class="login-label padtop10">
					<?php echo $lang['description']?>:
				</p>
				<p class="padbottom2">
					<textarea name="description" cols="35" rows="3"><?php echo $group['description']?></textarea>
				</p>
				
				<p class="login-label padtop10">
					<?php echo $lang['list_zone']?>: <span class="require">*</span>
				</p>
				<p class="padbottom2">
					<select name="lstzone[]" id="lstzone" multiple="multiple" style="width:305px;height:200px">
					<?php foreach ($lstZone as $item){?>
						<option value="<?php echo $item['zoneid']?>" title="<?php echo $item['zonename']?>" <?php if(in_array($item['zoneid'],$group['zones'])) echo 'selected="true"'?>>
							<?php echo $item['zonename']?>
						</option>
					<?php }?>
					</select>
				</p>
				
				<p class="login-label padtop10">
					<?php echo $lang['allow_free']?>: &nbsp; &nbsp; &nbsp; &nbsp;
					<label for="allowfree1">
						<input type="radio" <?php if($group['allowfree']==1) echo 'checked="true"'?> name="allowfree" id="allowfree1" value="1" /> <?php echo $lang['tu_duyet']?>
					</label> &nbsp; &nbsp; &nbsp; &nbsp;
					<label for="allowfree0">
						<input type="radio" <?php if($group['allowfree']==0) echo 'checked="true"'?> name="allowfree" id="allowfree0" value="0" /> <?php echo $lang['selfserving']?>
					</label>
				</p>
				
				<p class="login-label padtop10">
					<input type="hidden" name="groupid" value="<?php echo $group['id']?>" />
					<input type="submit" value="<?php echo $lang['save']?>" /> &nbsp;
					<input type="button" value="<?php echo $lang['cancel']?>" onclick="window.location.href='<?php echo site_url('user/listgroup')?>'" />
				</p>
			</form>
		</div>
	</div>
	<br />
</div>

<script language="javascript">
function checkSubmit()
{
	if($.trim($('#groupname').val()).length == 0)
	{
		alert('<?php echo $lang['enter_groupname']?>');
		$('#groupname').focus();
		return false;
	}

	if($('#lstzone option').filter(':selected').length == 0)
	{
		alert('<?php echo $lang['select_zone']?>');
		$('#lstzone').focus();
		return false;
	}

	return true;
}
function disabled_agency(groupid)
{
    var temp=confirm('<?php echo $lang['disable_confirm']?>');
    if (temp==true)
    {
        var strURL="<?php echo site_url('user/disable_agency?groupid=');?>"+groupid;
        var success = function(data)
        {
        	if(data=='error')
            {
                alert('<?php echo $lang['disable_error']?>')
            }else if(data=='unlogin'){
                window.location = '<?php echo site_url('login')?>';
            }else{
                alert('<?php echo $lang['disable_success']?>');
                window.location.reload();
            }
        };
        getAjax(strURL,"",'', '', '', false, success);
    }
}
</script>